<%@ page import="org.bson.Document" %>
<%@ page import="com.mongodb.client.*" %>
<%@ page import="org.bson.conversions.Bson" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.LinkedList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%


    request.setCharacterEncoding("utf-8");
    //update(quert,update,upsert)
    Document query = new Document();
    Document update = new Document();

    Integer _id = Integer.parseInt(request.getParameter("_id"));
    query.append("_id", _id);

    String name = request.getParameter("name");
    if (name != null && name != "") {
        update.append("name", name);
    }


    String sex = request.getParameter("sex");
    if (sex != null && sex != "") {
        update.append("sex", sex);
    }

    Integer age = Integer.parseInt(request.getParameter("age"));
    if (age != 0) {
        update.append("age", age);
    }

    String idCard = request.getParameter("idCard");
    if (idCard != null && idCard != "") {
        update.append("idCard", idCard);
    }

    String birthDay = request.getParameter("birthDay");
    if (birthDay != null && birthDay != "") {
        update.append("birthDay", birthDay);
    }

    String phone = request.getParameter("phone");
    if (phone != null && phone != "") {
        update.append("phone", phone);
    }

    String password = request.getParameter("password");
    if (password != null && password != "") {
        update.append("password", password);
    }

    String email = request.getParameter("email");
    if (email != null && email != "") {
        update.append("email", email);
    }

    String major = request.getParameter("major");
    if (major != null && major != "") {
        update.append("major", major);
    }

    String address = request.getParameter("address");
    if (address != null && address != "") {
        update.append("address", address);
    }


    String url = "mongodb://g08:abc%408@mongo.zhledu.cn:27017/g08";
    MongoClient client = MongoClients.create(url);
    MongoIterable<String> names = client.listDatabaseNames();
    MongoDatabase g08 = client.getDatabase("g08");
    MongoCollection<Document> collection = g08.getCollection("student");

    collection.updateOne(query, new Document("$set", update));
    response.getWriter().write("<script language=JavaScript>{window.alert('修改成功!');window.location.href='index.jsp'}</script>");


%>
